package com.sims.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sims.dao.ISimsTeacherDao;
import com.sims.eo.SimsTeacher;
import com.sims.util.HibernateSessionFactory;

public class SimsTeacherDaoImp implements ISimsTeacherDao {

	@Override
	public SimsTeacher getSimsTeacherbyTeacherNumber(String teacherNumber) {
		// 从数据库获取对象
		SimsTeacher simsTeacher = null;
		String sql = "from SimsTeacher s where s.teacherNumber=?";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setParameter(0, teacherNumber);
		List teachers = query.list();
		if (teachers.size() != 0) {
			simsTeacher = (SimsTeacher) teachers.get(0);
		}
		HibernateSessionFactory.closeSession();
		return simsTeacher;
	}

	@Override
	public List<SimsTeacher> getAllSimsTeacher() {
		String sql = "from SimsTeacher";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		List<SimsTeacher> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public boolean deletSimsTeacherById(Long id) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 查询删除对象
			SimsTeacher teacher = (SimsTeacher) session.get(SimsTeacher.class,
					id);
			// 持久化操作
			session.delete(teacher);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public SimsTeacher getSimsTeacherById(Long id) {
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 查询删除对象
		SimsTeacher teacher = (SimsTeacher) session.get(SimsTeacher.class, id);
		return teacher;
	}

	@Override
	public boolean updateSimsTeacher(SimsTeacher teacher) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.update(teacher);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public List<SimsTeacher> getAllSimsTeacher(int pageNo, int pageSize) {
		String sql = "from SimsTeacher";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<SimsTeacher> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public long getRowCount() {
		String hql = "select count(id) from SimsTeacher";
		long count = 0;
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long getRowCountByTeacherNumber(String teacherNumber) {
		String hql = "select count(teacherNumber) from SimsTeacher s where s.teacherNumber=?";
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		query.setParameter(0, teacherNumber);
		long count1 = 0;
		count1 = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		System.out.println(count1);
		return count1;
	}

	@Override
	public long createTeacher(SimsTeacher teacher) {
		Long id = 0L;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.save(teacher);
			// 提交事务
			trans.commit();
			id = teacher.getId();
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return id;
	}
}
